// ##############################
// ####  note:  bokeweilai log main fun
// ####  create:  TANWENHUI (twh:谭文辉)
// ####  date:   2024/6/18
// ##############################

#include "bkwl_common.h"
#include "bkwl_log.h"
#include <stdarg.h> // 调用头文件
#include <stdlib.h>
#include <stdio.h>
#include "../../../../../../../bk_idk/components/bk_system/printf_impl.h"

static char *pLogBuf = NULL;
bk_err_t bkwl_log_init()
{
	bk_err_t ret = BK_OK;
	printf("bkwl_log_init entry\n\n");
	pLogBuf = (char *)malloc(sizeof(char) * LOG_BUF_SIZE);
	printf("bkwl_log_init exit\n\n");
	return ret;
}

void tprintf(const char *fmt, ...)
{

#if BKWL_LOG_CUR == BKWL_LOG_NULL
#elif BKWL_LOG_CUR == BKWL_LOG_DEBUG
	if (pLogBuf == NULL)
		pLogBuf = (char *)malloc(sizeof(char) * LOG_BUF_SIZE);
	va_list args;
	va_start(args, fmt);
	memset(pLogBuf, 0, sizeof(char) * LOG_BUF_SIZE);
	vsnprintf(pLogBuf, LOG_BUF_SIZE - 1, fmt, args);
	va_end(args);
	printf("%s", pLogBuf);

#else
	if (!printf_is_init())
		return;
	va_list args;
	va_start(args, fmt);
	char string[LOG_BUF_SIZE];
	vsnprintf(&string[0], LOG_BUF_SIZE, fmt, args);
	string[LOG_BUF_SIZE - 1] = 0;
	uart_write_string(bk_get_printf_port(), string);
	va_end(args);
#endif
}
